using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.SignalR;

namespace Aurora.SingalR.Hubs;

/// <summary>
/// hub route method
/// </summary>
public static class HubRouteMethod
{
    /// <summary>
    /// 获取路由
    /// </summary>
    /// <param name="type">类型</param>
    /// <returns></returns>
    public static string GetHubRoute(this Type type)
    {
        string route = string.Empty;
        var attribute = type.GetCustomAttribute<HubRoute>(true);
        if (attribute != null)
        {
            route = attribute.Route;
        }

        return route;
    }

    /// <summary>
    /// 映射 hub
    /// </summary>
    /// <param name="app"></param>
    /// <typeparam name="T"></typeparam>
    public static void MapHubs<T>(this IEndpointRouteBuilder app) where T : Hub
    {
        var type = typeof(T);
        app.MapHub<T>(type.GetHubRoute());
    }
}